<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


class Task extends Base
{
    const TASK_LIST_POND = 'TASK:SUPER:LIST:POND';//超级任务池子
    const TASK_USER_REFRESH_NUM = 'TASK:USER:REFRESH:NUM';//用户刷新次数
    const TASK_USER_REFRESH_INFO = 'TASK:USER:REFRESH:SUPER:INFO';//用户刷新超级任务信息
    const TASK_USER_COMPLETE_NUM = 'TASK:USER:COMPLETE:NUM';//用户任务完成数量
    const TASK_USER_FREE_INFO = 'TASK:USER:FREE:INFO';//用户免费任务信息
    const TASK_USER_ENERGY_NUMBER = 'TASK:USER:ENERGY:NUMBER';//用户赋能值
    const TASK_USER_AWARD_LOG = 'TASK:USER:AWARD:LOG';//用户领取记录
    const award_gift_id = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,];//奖池任务id
    protected $activity_redis = '';// 活动redis db
    protected $api_redis = '';// api redis db


    public function __construct(array $attributes = [])
    {
        $this->activity_redis = env('REDIS_DB');
        $this->api_redis = env('API_REDIS_DB');
        Redis::select($this->activity_redis);
        parent::__construct($this->table, $attributes);
    }


    //生成奖池
    public function createPond()
    {
        $key = self::TASK_LIST_POND;
        $task_ids = self::award_gift_id;
        $num = [10, 10, 100, 200, 200, 200, 780, 1000, 1000, 800];
        $pond_arr = [];
        foreach ($num as $k => $v) {
            $arr = array_fill(0, $v, $task_ids[$k]);
            $pond_arr = array_merge($pond_arr, $arr);
        }
        shuffle($pond_arr);
        Redis::LPUSH($key, $pond_arr);
        return $this->ttlRedisKey($key);
    }

    public function getAllPond()
    {
        $key = self::TASK_LIST_POND;
        return Redis::lrange($key, 0, -1);
    }

    //出奖池
    public function popPond()
    {
        $key = self::TASK_LIST_POND;
        return Redis::LPOP($key);
    }

    //删除奖池
    public function delPond()
    {
        $key = self::TASK_LIST_POND;
        return Redis::DEL($key);
    }

    //获取用户刷新次数
    public function getUserRefreshNum($user_id)
    {
        $key = self::TASK_USER_REFRESH_NUM . date('ymd');
        return Redis::HGET($key, $user_id) ?? 0;
    }

    //用户刷新
    public function refreshTaskNum($user_id)
    {
        $key = self::TASK_USER_REFRESH_NUM . date('ymd');
        $v = Redis::HINCRBY($key, $user_id, '1');
        return $this->ttlRedisKey($key, 1296000);
    }

    //用户超级任务
    public function refreshSuperUserTask($user_id, $val)
    {
        $key = self::TASK_USER_REFRESH_INFO . date('ymd');
        $v = Redis::HSET($key, $user_id, $val);
        return $this->ttlRedisKey($key, 1296000);
    }

    //更新用户超级任务
    public function setRefreshSuperUserTask($user_id, $val)
    {
        $key = self::TASK_USER_REFRESH_INFO . date('ymd');
        $v = Redis::HSETNX($key, $user_id, $val);
        return $this->ttlRedisKey($key, 1296000);
    }

    //获取用户任务
    public function getSuperUserTask($user_id)
    {
        $key = self::TASK_USER_REFRESH_INFO . date('ymd');
        return Redis::HGET($key, $user_id);
    }

    public function getTaskCompleteNum($user_id)
    {
        $key = self::TASK_USER_COMPLETE_NUM;
        return Redis::HGET($key, $user_id) ?? 0;
    }

    //设置完成任务数量
    public function setTaskCompleteNum($user_id)
    {
        $key = self::TASK_USER_COMPLETE_NUM;
        $vel = Redis::HINCRBY($key, $user_id, '1');
        return $this->ttlRedisKey($key, 1296000);
    }

    //获取用户免费任务
    public function getUserFreeTask($user_id)
    {
        $key = self::TASK_USER_FREE_INFO . date('ymd');
        return Redis::HGET($key, $user_id) ?? '';
    }

    //用户赋能值分配
    public function setEnergyNumber($user_id)
    {
        $key = self::TASK_USER_ENERGY_NUMBER;
        $v = rand(1, 10);
        Redis::HSETNX($key, $user_id, $v);
        return $this->ttlRedisKey($key, 1296000);
    }

    //获取用户赋能值
    public function getEnergyNumber($user_id = 0)
    {
        $key = self::TASK_USER_ENERGY_NUMBER;
        $data = Redis::HGET($key, $user_id);
        if ($user_id == 0) {
            $data = Redis::HGETALL($key);
        }
        return $data;
    }


    //用户领取记录
    public function setAwardLog($user_id, $diamond)
    {
        $key = self::TASK_USER_AWARD_LOG;
        $this->ttlRedisKey($key, 1296000);
        return Redis::HSETNX($key, $user_id, $diamond);
    }

    //获取用户领取记录
    public function getAwardLog($user_id)
    {
        $key = self::TASK_USER_AWARD_LOG;
        return Redis::HGET($key, $user_id);
    }

    //设置免费任务信息
    public function setUserFreeTask($user_id, $data)
    {
        $key = self::TASK_USER_FREE_INFO . date('ymd');
        Redis::HSET($key, $user_id, $data);
        return $this->ttlRedisKey($key, 1296000);
    }

    //获取火箭
    public function getRocket($start_time)
    {
        $key = 'CACHE:USER:ROCKET_RECOMMEND:LIST';
        Redis::select($this->api_redis);
        $start_time = $start_time . '000';
        $end_time = strtotime(date('Y-m-d 23:59:59'));
        $end_time = $end_time . '999';
        $data = Redis::ZRANGEBYSCORE($key, $start_time, $end_time);
        Redis::select($this->activity_redis);
        return $data;
    }

    //获取开启小屋
    public function getOpenRoom($user_id)
    {
        $key = 'CACHE:ROOM:OPEN_RECORD:0:' . date('Ymd');
        Redis::select($this->api_redis);
        $data = Redis::HGET($key, $user_id);
        Redis::select($this->activity_redis);
        return $data;
    }

    //获取神秘邂逅
    public function getMeet($user_id)
    {
        $key = 'CACHE:MEET:RECORD:' . date('Ymd');
        Redis::select($this->api_redis);
        $data = Redis::HGET($key, $user_id);
        Redis::select($this->activity_redis);
        return $data;
    }

    //获取上麦次数
    public function getJoinRoom($user_id)
    {
        $key = 'CACHE:ROOM:INTO_MICRO_RECORD:' . date('Ymd');
        Redis::select($this->api_redis);
        $data = Redis::HGET($key, $user_id);
        Redis::select($this->activity_redis);
        return $data;
    }

    //获取所有超级任务用户
    public function getAllSuperTaskUser()
    {
        $key = self::TASK_USER_REFRESH_INFO . date('ymd');
        return Redis::HGETALL($key);
    }

    //获取所有免费任务用户
    public function getFreeTaskUser()
    {
        $key = self::TASK_USER_FREE_INFO . date('ymd');
        return Redis::HGETALL($key);
    }

    //用户赋能值分配
    public function setEnergyNumberTest($user_id, $k)
    {
        $key = self::TASK_USER_ENERGY_NUMBER . $k;
        $v = rand(1, 10);
        Redis::HSETNX($key, $user_id, $v);
        return $this->ttlRedisKey($key, 1296000);
    }

    //获取用户赋能值
    public function getEnergyNumberTest($user_id = 0, $k)
    {
        $key = self::TASK_USER_ENERGY_NUMBER . $k;
        $data = Redis::HGET($key, $user_id);
        if ($user_id == 0) {
            $data = Redis::HGETALL($key);
        }
        return $data;
    }
}
